package magics
{
	import flash.utils.Dictionary;
	
	import utils.StringUtil;

	public class MagicData
	{
		public function MagicData()
		{
		}
		public var name:String = "";
		public var x:Number = 0;
		public var y:Number = 0;
		public var alpha:Number = 1;
		public var rotation:int = 0;
		public var scaleX:Number = 1;
		public var scaleY:Number = 1;
		public var playState:int = 0;//0不变，1播放，2停止
		public var cacheType:String = "mc";
		public var blendMode:String = "";
		public var cacheWidth:int = -1;
		public var cacheHeight:int = -1;
		
		private static function convert(ani:XML):Object{
			var aniData:Array = [];
			var magicData:Dictionary;
			for each(var a:XML in ani.children()){
				if(a.name() == "frame"){
					if(undefined != a.@name)
					{
						var frameIndex:int = int(a.@name);
						aniData[frameIndex] = [];
						for each(var b:XML in a.children()){
							aniData[aniData.length - 1].push(getBoneData(b));
						}
					} 
				}
				
				//法术文件
				/*if(a.name() == "magic"){
				magicData =  MagicData.convert(a);
				}*/
			}
			return {ani:aniData,magic:magicData};
		}
		
		//将动作数据转为帧数据
		public static function convertAtoF(data:Object):Object{
			var ani:XML = StringUtil.readXML(data);
			if(ani == null){
				return [];
			}else{
				return convert(ani);
			}
		}
		
		//获得单个骨骼图片资源的信息
		public static function getBoneData(data:XML):MagicData{
			var boneData:MagicData = new MagicData();
			boneData.name = data.@name;
			if(data.@x != undefined){
				boneData.x = int(data.@x);
			}
			if(data.@y != undefined){
				boneData.y = int(data.@y);
			}
			if(data.@scaleX != undefined){
				boneData.scaleX = Number(data.@scaleX);
			};
			if(data.@scaleY != undefined){
				boneData.scaleY = Number(data.@scaleY);
			}
			if(data.@rotation != undefined){
				boneData.rotation = Number(Math.round(data.@rotation));
			}
			if(data.@alpha != undefined){
				boneData.alpha = Number(data.@alpha);
			}
			if(data.@cacheType != undefined){
				boneData.cacheType = data.@cacheType;
			}
			if(data.@blendMode != undefined){
				boneData.blendMode = data.@blendMode;
			}
			if(data.@cacheWidth != undefined){
				boneData.cacheWidth = Number(data.@cacheWidth);
			}
			if(data.@cacheHeight != undefined){
				boneData.cacheHeight = Number(data.@cacheHeight);
			}
			return boneData;
		}
	}
}